﻿using System.Collections.Generic;
using WS.EKA.DataAccess;
using WS.EKA.Model;
using WS.EKA.ServiceInterface;
using WS.EKA.Utilities;
using WS.EKA.Utilities.Consts;
using System.Text;

namespace WS.EKA.ServiceImpl
{
    public class ProductCatagoryService : IProductCatagory
    {
        public List<ProductCatagory> GetCatagoryByFatherID(int id)
        {
            var catagoryCache = CacheManager.Instance.GetObjectFromMemoryCache(CacheKey.ProductCatagory) as List<ProductCatagory>;
            var catagoryList = ProductCatagoryContext.GetByConditions(x => x.FatherID == id, null, catagoryCache);
            foreach (var catagory in catagoryList)
            {
                var subCatagoryList = ProductCatagoryContext.GetByConditions(x => x.FatherID == catagory.ID, null, catagoryCache);
                if (subCatagoryList == null || subCatagoryList.Count==0)
                {
                    catagory.IsLastLevel = true;
                    continue;
                }
                StringBuilder sb = new StringBuilder(250);
                subCatagoryList.ForEach((x) =>
                    {
                        sb.Append(x.Name+" ");
                    });
                catagory.Description = sb.ToString();
            }
            return catagoryList;
        }

    }
}
